package practtica4;


import BBDD.GestorBBDD;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;



@SuppressWarnings("serial")
public class V3AcontAnadirBBDDCont extends javax.swing.JFrame
{
	//ATRIBUTOS
            private javax.swing.JButton jButton1;
	    private javax.swing.JButton jButton2;
	    private javax.swing.JButton jButton3;
	    private javax.swing.JButton jButton4;//alergias
	    private javax.swing.JScrollPane jScrollPane1;
	    private javax.swing.JTable tablaResultados;
            private String personaje;
            private GestorBBDD gestor;
           private int idcolec;
            String[] hombrecillos;
            int iddoc;
	   
	//CONSTRUCTORA 
    public V3AcontAnadirBBDDCont(String personaje) {
        this.personaje=personaje;
        this.jButton2=new JButton();        
    	initComponents();
    	this.setLocation(0,0);
        this.setSize(1250,650);
        this.setLocationRelativeTo(null);
        this.setResizable(false);

    }

    V3AcontAnadirBBDDCont(String[] hombrecillos, int iddoc) {
        this.hombrecillos=hombrecillos;
        this.iddoc=iddoc;
        this.jButton2=new JButton();        
    	initComponents();
    	this.setLocation(0,0);
        this.setSize(1250,650);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
    }
    
    //MÉTODOS
    private void initComponents() {
    	jScrollPane1 = new JScrollPane();
        tablaResultados = new JTable();
        jButton2 = new JButton();
        jButton3 = new JButton();
        jButton1 = new JButton();
        jButton4 = new JButton();
        gestor= new GestorBBDD();
        
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Feed&Fitness: Menú Colectivo");

     
        
        Object[][] o=crearTabla(1);String[]cabecera=cabecera();
   	tablaResultados.setModel(new DefaultTableModel(o,cabecera));
    
      
       this.rellenarCamposTabla(hombrecillos, 0);
       
       tablaResultados.setGridColor(Color.green);
       	
       	tablaResultados.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
					int col=tablaResultados.columnAtPoint(e.getPoint());
					int fila=tablaResultados.rowAtPoint(e.getPoint());
					
						if(fila>=0 && col>=0 /*&& no salirme de limites maxFila*/){
							String cole=(String)tablaResultados.getValueAt(fila, col);
                                                        if (!cole.equals("")){
                                                        try {
                                                        idcolec = gestor.dameIDColecPorNombre(cole);
                                                        } catch (ClassNotFoundException ex) {
                                                            Logger.getLogger(V3PersAnadirBBDDCont.class.getName()).log(Level.SEVERE, null, ex);
                                                        } catch (SQLException ex) {
                                                            Logger.getLogger(V3PersAnadirBBDDCont.class.getName()).log(Level.SEVERE, null, ex);
                                                        }
                                                        }
                                                        else {JOptionPane.showMessageDialog(null,"ERROR!!Seleccione una celda correcta");
                                                        Logger.getLogger(V3PersAnadirBBDDCont.class.getName()).log(Level.SEVERE, null, personaje);
                                                        }
                                                        }
						else JOptionPane.showMessageDialog(null,"ERROR!!Seleccione una celda correcta");
                                                    Logger.getLogger(V3PersAnadirBBDDCont.class.getName()).log(Level.SEVERE, null, personaje);
                                        	}
        });
        
        jScrollPane1.setViewportView(tablaResultados);
        
        jButton2.setText("  Aceptar  ");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    jButton2ActionPerformed(evt);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(V3PersAnadirBBDDCont.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SQLException ex) {
                    Logger.getLogger(V3PersAnadirBBDDCont.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2)
                .addGap(55)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 660, Short.MAX_VALUE)
               .addContainerGap())
            .addComponent(jScrollPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE)
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton2)))
        );

        pack();

    }// </editor-fold>//GEN-END:initComponents

    private Object[][] crearTabla(int numsemanas){
    	int filas=hombrecillos.length;
    	int columnas=1;
    	Object[][] o=new Object[filas][columnas];
    	return o;
    }
    
    private String[] cabecera(){
    	String[] s=new String [] {"Nombre"};
    	return s;
    }


	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) throws ClassNotFoundException, SQLException { //ATRAS
		gestor.anyadirDocTieneAcont(iddoc,idcolec);
                V3 v3= new V3(iddoc);
                v3.setVisible(true);
                this.dispose();
		
	}

    private void rellenarCamposTabla(String[] hombrecillos, int i) {
        int pos=0;
        int fila=i;
        for(int j=0;j<hombrecillos.length-1;j++){
            tablaResultados.setValueAt(hombrecillos[pos], fila, 0);
            pos++;
            fila++;
    
        }
        
    }
	
	
}